package GUI;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

import mainPackage.Cronometro;
import parser.FunctionSyntacticException;
import parser.ParserConsulta;
import parser.consulta.Consulta;
import parser.consulta.ConsultaCompuesta;
import parser.simbolo.delimitador.Delimitador;
import parser.simbolo.delimitador.Parentesis1;
import parser.simbolo.delimitador.Parentesis2;
import parser.simbolo.operador.And;
import parser.simbolo.operador.Not;
import parser.simbolo.operador.Operador;
import parser.simbolo.operador.Or;
import structuresManagers.BplusTreeManager;
import structuresManagers.SignatureManager;
import FilesManagers.CantOpenFileException;
import Filters.FullFilter;

public abstract class AbstractThreadManager extends Thread{

	@SuppressWarnings("deprecation")
	 static String arbolFile ="arbol.txt";
	 static String signatureFile ="firmas.txt";
	 BplusTreeManager indexManager;
	 SignatureManager signatureManager;
	 Consulta c;
	 List<Operador> listaOperandos;
	 List<Delimitador> listaDelimitadores;
	 Cronometro cronometro;
	
	
	public AbstractThreadManager(){
			
			
			try {
				FullFilter filtro = new FullFilter("./conf/stopwords.conf","./conf/stemming.conf","./conf/badcharacters.txt");
				indexManager = new BplusTreeManager("urls.txt","urlIndexadas.txt",filtro);
				signatureManager = new SignatureManager("urls.txt","urlFirmadas.txt",filtro);
				
				listaOperandos = new ArrayList<Operador>();
				listaOperandos.add(new Or());
				listaOperandos.add(new And());
				listaOperandos.add(new Not());
				
				listaDelimitadores = new ArrayList<Delimitador>();
				listaDelimitadores.add(new Parentesis1());
				listaDelimitadores.add(new Parentesis2());	
				
				
				
				/* inicializacion de las consultas booleanas */
				
				
				//SignatureFileManager signatureFileManager = new SignatureFileManager("urls.txt",filtro);
				//in = new BufferedReader(new InputStreamReader(System.in));
				cronometro = new Cronometro();
			} catch (CantOpenFileException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
		
	public abstract void run();
	
		
	
	public static boolean arbolExiste() {
		try {
			RandomAccessFile file = new RandomAccessFile(arbolFile,"r");
			file.close();
		} catch (FileNotFoundException e) {
			return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}

	public static boolean signatureFileExiste() {
		try {
			RandomAccessFile file = new RandomAccessFile(signatureFile,"r");
			file.close();
		} catch (FileNotFoundException e) {
			return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}

	public static boolean isValidNodeSize(Integer integer) {
		if(integer>=128){
			if(Math.floor((Math.log(integer) / Math.log(2)))==(Math.log(integer) / Math.log(2))){
				return true;
			}
		}
		return false;
	}
	public static boolean isValidNodeSize(String s) {
		try{
			
			return isValidNodeSize(Integer.valueOf(s));
		}catch(NumberFormatException e){
			return false;
			
		}
	}
	public static boolean isValidHashNumber(String cantHash){
		if (cantHash.equals("1")
				|| cantHash.equals("2")
	        	|| cantHash.equals("3")
	        	|| cantHash.equals("4")
	        	|| cantHash.equals("5")
	        	|| cantHash.equals("6")
	        	|| cantHash.equals("7")){
		    	return false;	
	        }
		return true;
	}
	
}
